# -*- coding: UTF-8 -*-

from configure import config
#from util.tornado_qiniu_util import get_qiniu_private_download_url

local_base_src  = config.MEDIA_BASE_URL + "/"
cdn_base_src    = config.FILE_CDN_URL + "/"

use_qiniu = config.UPLOAD_FILE_TO_QINIU

def get_image_url_by_size(md5, size = "full", use_cdn = True, ext="zip"):
    # TODO default image
    if not md5:
        return ""

    if use_cdn and use_qiniu:
        return get_cdn_image_url(md5, str(size))
    else:
        return get_local_image_url(md5, str(size), ext)

def get_cdn_image_url(md5, size):
    ext = get_file_ext(md5)
    if size == "full":
        return "%s%s.%s" % (cdn_base_src,md5,ext)
    else:
        return "%s%s.%s?imageView2/0/q/80/w/%s" % (cdn_base_src,md5,ext,size)

def get_local_image_url(md5, size, ext='zip'):
    '''
    ext = get_file_ext(md5)
    if not ext:
        ext = "jpg"
    '''

    if md5.startswith("static"):
        return "%s%s"%(local_base_src,md5)
    else:
        return "%smedia01/%s-%s.%s" %(
            local_base_src,
            md5,
            size,
            ext
        )

def get_file_ext(md5):
    if md5:
        temp_list = md5.split("_")
        if len(temp_list) == 1:
            return ""
        return temp_list[1]
    return ""

def get_cdn_image_url_list(md5s,size = "full"):
    results = []
    for md5 in md5s:
        result = get_image_url_by_size(md5, size)
        results.append(result)

    return results

def get_private_download_url(md5, expire = 1800):
    if use_qiniu:
        pass
        #return get_qiniu_private_download_url(md5, expire)
    else:
        return get_local_image_url(md5, "full")
